package com.zxh.blog.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by server on 2017/7/17.
 */
public final class DateUtil {
	private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

	//时间戳转datatime
	public static String noToTime(String no) {
		String formats = "yyyy-MM-dd HH:mm:ss";
		Long timestamp = Long.parseLong(no) * 1000;
		String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
		return date;
	}

	//生成当前时间的datatime格式
	public static String getStamp(){
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String stamp = dateFormat.format(date);
		return stamp;
	}

	//datatime转化为8位格式
	public static String stampToDate(String stamp){
		String date = "";
		if(StringUtil.isNotNull(stamp)){
			Date date1 = null;
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				////parse:把String型的字符串转换成特定格式的date类型
				date1 = dateFormat.parse(stamp);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			date = format.format(date1);
		}
		return date;
	}

	//8位格式转化为datatime
	public static String dateToStamp(String date){
		String stamp = "";
		if(StringUtil.isNotNull(date)){
			Date date1 = null;
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
			try {
				////parse:把String型的字符串转换成特定格式的date类型
				date1 = dateFormat.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			stamp = format.format(date1);
		}
		return stamp;
	}

	//性别转换,男女转F,M
	public static String toOnline(String gender){
		String genderJudge = "";
		if (gender.equals("M")) {
			genderJudge = "1";
		} else if (gender.equals("F")) {
			genderJudge = "0";
		} else {
			genderJudge = "";
		}
		return genderJudge;
	}

	//性别转换,男女转1,2
	public static String toOffline(String gender){
		String genderJudge = "";
		if (gender.equals("1")) {
			genderJudge = "M";
		} else if (gender.equals("0")) {
			genderJudge = "F";
		} else {
			genderJudge = "";
		}
		return genderJudge;
	}

}
